﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class CorpseCharacter : Loot, ICorpse
    {
        public Character MyCharacter;
        public Character CarrierCharacter = null;

        public CorpseCharacter(AppHandler2 appHandler, Character character)
            : base(appHandler)
        {
            MyCharacter = character;
            this.Description = "Double click to loot";
            this.ItemType = "Corpse";
            this.timerDestroyInterval = 20 * 60 * 1000;
        }

        public List<Loot> RemoveLoot() 
        {
            List<Loot> ItemsToDrop = new List<Loot>();

            List<Loot> BagItems = MyCharacter.bagManager.removeAllBagItems();
            foreach (Loot loot in BagItems)
                ItemsToDrop.Add(loot);

            if (BagItems.Count > 0)
                MyCharacter.bagManager.UpdateBag();

            if (MyCharacter.Coins > 0)
            {
                Coins coins = new Coins(MyCharacter.GameHandler);
                coins.NumCoins = MyCharacter.Coins;
                coins.Bag = true;
                coins.CanConsume = false;
                coins.ID = -3;
                coins.GameUID = MyCharacter.GameHandler.ItemIDGen.GetUID();
                coins.ImageURL = "Coin";
                coins.ItemType = "Coins";
                coins.Name = MyCharacter.Coins.ToString() + " Coins";

                MyCharacter.Coins = 0;
                MyCharacter.Update_Character();

                ItemsToDrop.Add(coins);
            }

            return ItemsToDrop;
        }

        public void MoveGhost(MapItem moveToMapItem)
        {
            //This method should only get called from inside a method that has already locked the map (No need to lock)

            MyCharacter.currentMapItem.CreaturesBases.Remove(MyCharacter);
            MyCharacter.SetPosition(moveToMapItem);
            
            moveToMapItem.CreaturesBases.Add(MyCharacter);

            //We also dont need to update nearby characters because whatever function called MoveGhost
            //should already be updating the tile we moved from and to
            MyCharacter.Update_Character();
            MyCharacter.AddMapItems(moveToMapItem.mapItemsWithin8, true);
        }
    }
}
